/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.rum.gui;

import cz.muni.fi.rum.RumConfigurator;
import javax.swing.JLabel;

/**
 *
 * @author pmikulasek
 */
public class FieldValidator {

    private static FieldValidator instance;

    private FieldValidator() {
    }

    public static FieldValidator getInstance() {
        if (instance == null) {
            instance = new FieldValidator();
        }
        return instance;
    }

    public Integer controlAndParsePortInteger(JLabel feedBackLabel, String labelName, String port, boolean requiered) {
        //validace portu muze byt 0 to 65535
        //TODO ===Validator=== zpravit hlasky
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(RumConfigurator.class).getContext().getResourceMap(FieldValidator.class);
        if (port.equals("")) {
            if (requiered) {
                feedBackLabel.setText(feedBackLabel.getText() + labelName + resourceMap.getString("InputExceptionBox.PortRequeredException") + "<br/>");
            }
            return null;
        } else {
            try {
                int intPort = Integer.parseInt(port);
                if (intPort >= 0 && intPort < 65536) {
                    return intPort;
                } else {
                    feedBackLabel.setText(feedBackLabel.getText() + labelName + "\"" + port + "\" " + resourceMap.getString("InputExceptionBox.PortRangeException") + "<br/>");
                }
            } catch (NumberFormatException ex) {
                feedBackLabel.setText(feedBackLabel.getText() + labelName + "\"" + port + "\" " + resourceMap.getString("InputExceptionBox.PortNumberException") + "<br/>");
            }
        }
        return null;
    }

    public Long controlAndParseDataSizeInteger(JLabel feedBackLabel, String labelName, String dataSize, boolean requiered) {
        //validace na kladne cislo
        //TODO ===Validator=== zpravit hlasky
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(RumConfigurator.class).getContext().getResourceMap(FieldValidator.class);
        if (dataSize.equals("")) {
            if (requiered) {
                feedBackLabel.setText(feedBackLabel.getText() + labelName + resourceMap.getString("InputExceptionBox.DataSizeRequieredException") + "<br/>");
            }
            return null;
        } else {
            try {
                Long intDataSize = Long.parseLong(dataSize);
                if (intDataSize > 0) {
                    return intDataSize;
                } else {
                    feedBackLabel.setText(feedBackLabel.getText() + labelName + "\"" + dataSize + "\" " + resourceMap.getString("InputExceptionBox.NegativeNumberException") + "<br/>");
                }
            } catch (NumberFormatException ex) {
                feedBackLabel.setText(feedBackLabel.getText() + labelName + "\"" + dataSize + "\" " + resourceMap.getString("InputExceptionBox.DataSizeNumberException") + "<br/>");
            }
        }
        return null;
    }

    private boolean controlNumberForServerAddress(int number) {
        return number >= 0 && number <= 255;
    }

    public String controlAndParseServerAddress(JLabel feedBackLabel, String labelName, String address, boolean requiered) {
        //TODO ===Validator=== zpravit hlasky
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(RumConfigurator.class).getContext().getResourceMap(FieldValidator.class);
        if (address.equals("")) {
            if (requiered) {
                feedBackLabel.setText(feedBackLabel.getText() + labelName + resourceMap.getString("InputExceptionBox.ServerAddressRequieredException") + "<br/>");
            }
            return null;
        } else {
            String numbers[] = address.split("\\.", 4);
            if (numbers.length != 4) {
                feedBackLabel.setText(feedBackLabel.getText() + labelName + "\"" + address + "\" " + resourceMap.getString("InputExceptionBox.ValidServerAddressException") + "<br/>");
            } else {
                try {
                    if (controlNumberForServerAddress(Integer.parseInt(numbers[0]))
                            && controlNumberForServerAddress(Integer.parseInt(numbers[1]))
                            && controlNumberForServerAddress(Integer.parseInt(numbers[2]))
                            && controlNumberForServerAddress(Integer.parseInt(numbers[3]))) {
                        return address;
                    } else {
                        feedBackLabel.setText(feedBackLabel.getText() + labelName + "\"" + address + "\" " + resourceMap.getString("InputExceptionBox.ValidServerAddressException") + "<br/>");
                    }
                } catch (NumberFormatException ex) {
                    feedBackLabel.setText(feedBackLabel.getText() + labelName + "\"" + address + "\" " + resourceMap.getString("InputExceptionBox.ValidServerAddressException") + "<br/>");
                }
            }
        }
        return null;
    }

    public Short controlAndParseTtl(JLabel feedBackLabel, String labelName, String ttl, boolean requiered) {
        //TODO ===Validator=== zpravit hlasky
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(RumConfigurator.class).getContext().getResourceMap(FieldValidator.class);
        if (ttl.equals("")) {
            if (requiered) {
                feedBackLabel.setText(feedBackLabel.getText() + labelName + resourceMap.getString("InputExceptionBox.TtlRequieredException") + "<br/>");
            }
            return null;
        } else {
            try {
                Short shortTtl = Short.parseShort(ttl);
                if (shortTtl.intValue() > 0) {
                    return shortTtl;
                } else {
                    feedBackLabel.setText(feedBackLabel.getText() + labelName + "\"" + ttl + "\" " + resourceMap.getString("InputExceptionBox.NegativeNumberException") + "<br/>");
                }
            } catch (NumberFormatException ex) {
                feedBackLabel.setText(feedBackLabel.getText() + labelName + "\"" + ttl + "\" " + resourceMap.getString("InputExceptionBox.TTLNumberException") + "<br/>");
            }
        }
        return null;
    }
}
